import AdmZip from 'adm-zip'
import path from 'path'
import fs from 'fs'

/**
 * create release zip package
 * @param name - name of the package
 * @param version - version of the package
 */
export default function rz(name: string = '-', version: string = '-') {
  if (version === '-' && name === '-') {
    const pJson = fs.readFileSync(path.resolve('package.json'), 'utf-8')
    const jsonData = JSON.parse(pJson)
    version = jsonData.version
    name = jsonData.name
  }
  const releaseDir = path.resolve('release')
  if (!fs.existsSync(releaseDir)) {
    throw new Error('release directory not found')
  }
  const zip = new AdmZip()
  zip.addLocalFolder(path.resolve('release/www'), 'www')
  zip.addLocalFile(path.resolve('release/server.bundle.js'))
  zip.addLocalFile(path.resolve('release/server.bundle.js.LICENSE.txt'))
  zip.writeZip(path.resolve(`release/${name}-${version}.zip`))
}
